import mainRouters from '@/routes/mainRouter'
import allRoutes from '@/routes/route-config';

const state = {
  routes: []
};

function filterRoutes(routes, role_id) {
  let filter1 = routes.filter(item => item.meta).filter(item => item.meta.role && item.meta.role.length > 0);
  return filter1.filter(item => {
    return item.meta.role.indexOf(role_id) !== -1
  });
}

const actions = {
  generatorRoutes({commit,rootState}) {
    return new Promise(resolve => {
      //
      // getName();
      // filterRoutes(路由,角色)
      const filterArr = filterRoutes(mainRouters, rootState.users.role_id)
      // 这个角色显示的路由
      allRoutes[2].children = filterArr;
      commit('SET_ROUTES', allRoutes);
      resolve(allRoutes);// 筛选过后的路由
    })
  }
};

const mutations = {
  SET_ROUTES(state,payload) {
    state.routes = payload;
  }

};


export default {
  state, actions, mutations, namespaced: true
}
